package com.rt.web.controller.system;

import java.util.List;

import com.rt.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rt.common.annotation.Log;
import com.rt.common.core.controller.BaseController;
import com.rt.common.core.domain.AjaxResult;
import com.rt.common.enums.BusinessType;
import com.rt.system.domain.RtExamType;
import com.rt.system.service.IRtExamTypeService;
import com.rt.common.utils.poi.ExcelUtil;
import com.rt.common.core.page.TableDataInfo;

/**
 * 考试类型Controller
 * 
 * @author Yg
 * @date 2022-01-05
 */
@RestController
@RequestMapping("/system/examtype")
public class RtExamTypeController extends BaseController
{
    @Autowired
    private IRtExamTypeService rtExamTypeService;

    /**
     * 查询考试类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:examtype:list')")
    @GetMapping("/list")
    public TableDataInfo list(RtExamType rtExamType)
    {
        startPage();
        List<RtExamType> list = rtExamTypeService.selectRtExamTypeList(rtExamType);
        return getDataTable(list);
    }

    /**
     * 查询考试类型列表
     */
    @GetMapping("/availableList")
    public TableDataInfo availableList(RtExamType rtExamType)
    {
        rtExamType.setStatus(1);
        List<RtExamType> list = rtExamTypeService.selectRtExamTypeList(rtExamType);
        return getDataTable(list);
    }
    /**
     * 导出考试类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:examtype:export')")
    @Log(title = "考试类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RtExamType rtExamType)
    {
        List<RtExamType> list = rtExamTypeService.selectRtExamTypeList(rtExamType);
        ExcelUtil<RtExamType> util = new ExcelUtil<RtExamType>(RtExamType.class);
        return util.exportExcel(list, "考试类型数据");
    }

    /**
     * 获取考试类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:examtype:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rtExamTypeService.selectRtExamTypeById(id));
    }

    /**
     * 新增考试类型
     */
    @PreAuthorize("@ss.hasPermi('system:examtype:add')")
    @Log(title = "考试类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RtExamType rtExamType)
    {
        if (StringUtils.isNotNull(rtExamTypeService.selectRtExamTypeByName(rtExamType))){
            return AjaxResult.error("考试或编码已存在");
        }
        rtExamType.setCreateOperator(getUsername());
        return toAjax(rtExamTypeService.insertRtExamType(rtExamType));
    }

    /**
     * 修改考试类型
     */
    @PreAuthorize("@ss.hasPermi('system:examtype:edit')")
    @Log(title = "考试类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RtExamType rtExamType)
    {
        if (StringUtils.isNotNull(rtExamTypeService.selectRtExamTypeByName(rtExamType))){
            return AjaxResult.error("考试或编码已存在");
        }
        rtExamType.setUpdateOperator(getUsername());
        return toAjax(rtExamTypeService.updateRtExamType(rtExamType));
    }

    /**
     * 删除考试类型
     */
    @PreAuthorize("@ss.hasPermi('system:examtype:remove')")
    @Log(title = "考试类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rtExamTypeService.deleteRtExamTypeByIds(ids));
    }
}
